<?php
namespace app\command;

use app\beautiful\model\order\BuyOrderModel;
use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\partner\PartnerShareModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\setting\ConsumeBonusModel;
use app\beautiful\model\setting\ConsumeBonusSettingModel;
use app\beautiful\model\setting\PartnerBonusSettingModel;
use app\beautiful\model\setting\PartnerTaskBonusSettingModel;
use app\common\model\DataRecordModel;
use app\common\model\MemberConsumeModel;
use app\common\model\ShopDetailModel;
use app\stores\model\order\GoodsOrderModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Partnerbonus extends Command
{
    protected function configure()
    {
        $this->setName('Partnerbonus')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $old_id=0;
        $page=1;
        $num=100;
        $order['a.partner_id']='asc';
        $h=date('H');
        $time=time();
        if($h=='00'){
            $time=time()-60;
        }
        //获取当天的开始和结束时间
        $day_start=strtotime(date('Y-m-d 00:00:00'));
        $day_end=strtotime(date('Y-m-d 23:59:59'));
        //获取本月的开始时间和结束时间
        $start_of_month = strtotime(date('Y-m-01 00:00:00',$time));
        $end_of_month = strtotime(date('Y-m-t 23:59:59',$time));
        //获取本季度的开始时间和结束时间
        $j_res=$this->jidu($time);
        $j_start=$j_res['startTime'];
        $j_end=$j_res['endTime'];
        do{
            $s=[];
            $s['partner_id']=$old_id;
            $list=PartnerModel::plist($s,$order,$page,$num);
            if(empty($list)){
                $output->writeln("暂无数据".var_export([$s,$order,$page],true) );exit();
            }
            foreach ($list as $v){
                $setting=PartnerBonusSettingModel::where(['shop_id'=>$v['shop_id'],'is_delete'=>0])->find();
                if(empty($setting)){
                    continue;
                }
                $role_ids=explode(',',$setting['join_bonus_role_id']);
                if(!in_array($v['role_id'],$role_ids)){
                    continue;
                }
                $u_s=[];
                $u_s['shop_id']=$v['shop_id'];
                $u_s['member_id']=$v['member_id'];
                $u_s['partner_id']=$v['partner_id'];
                $u_s['bonus_share']=$v['bonus_share'];
                $u_s['role_id']=$v['role_id'];
                $u_s['total_share_num']=$setting['total_share_num'];
                $u_s['bonus_cycle_type']=$setting['bonus_cycle_type'];
                $u_s['start_time']=$day_start;
                $u_s['end_time']=$day_end;
                $this->order_data($u_s,3);//更新天数据
                $u_s['start_time']=$start_of_month;
                $u_s['end_time']=$end_of_month;
                $this->order_data($u_s,1);//更新月数据
                $u_s['start_time']=$j_start;
                $u_s['end_time']=$j_end;
                $this->order_data($u_s,2);//更新季度数据
            }
            $end=end($list);
            $old_id=$end['partner_id'];
        }while(true);
    }

    public function jidu($time){
        $currentYear = date('Y',$time);
        $currentMonth = date('m',$time);
        if ($currentMonth >= 1 && $currentMonth <= 3) {
            $season = 1;
        } elseif ($currentMonth >= 4 && $currentMonth <= 6) {
            $season = 2;
        } elseif ($currentMonth >= 7 && $currentMonth <= 9) {
            $season = 3;
        } elseif ($currentMonth >= 10 && $currentMonth <= 12) {
            $season = 4;
        }

        switch ($season) {
            case 1:
                $startTime = "{$currentYear}-01-01 00:00:00";
                $endTime = "{$currentYear}-03-31 23:59:59";
                break;
            case 2:
                $startTime = "{$currentYear}-04-01 00:00:00";
                $endTime = "{$currentYear}-06-30 23:59:59";
                break;
            case 3:
                $startTime = "{$currentYear}-07-01 00:00:00";
                $endTime = "{$currentYear}-09-30 23:59:59";
                break;
            case 4:
                $startTime = "{$currentYear}-10-01 00:00:00";
                $endTime = "{$currentYear}-12-31 23:59:59";
                break;
        }
        $startTime=strtotime($startTime);
        $endTime=strtotime($endTime);
        return compact('startTime','endTime');
    }
    public function b_year(){
        $currentYear = date('Y');
        $currentMonth = date('m');

        if ($currentMonth >= 1 && $currentMonth <= 6) {
            $half = 1;
        } elseif ($currentMonth >= 7 && $currentMonth <= 12) {
            $half = 2;
        }

        switch ($half) {
            case 1:
                $startTime = "{$currentYear}-01-01 00:00:00";
                $endTime = "{$currentYear}-06-30 23:59:59";
                break;
            case 2:
                $startTime = "{$currentYear}-07-01 00:00:00";
                $endTime = "{$currentYear}-12-31 23:59:59";
                break;
        }
        $startTime=strtotime($startTime);
        $endTime=strtotime($endTime);
        return compact('startTime','endTime');
    }
    public function order_data($where,$type){
        $where['status']=8;
        $uo_info=GoodsOrderModel::user_order($where);
        $where['status']=1;
        $ub_info=BuyOrderModel::user_order($where);
        $where['status']=8;
        $g_money=GoodsOrderModel::max_money($where);
        $where['status']=1;
        $b_money=BuyOrderModel::max_money($where);
        $max_money=$g_money>$b_money?$g_money:$b_money;
        $total_money=$uo_info['money']+$ub_info['money'];
        $insert['partner_id']=$where['partner_id'];
        $insert['shop_id']=$where['shop_id'];
        $insert['status']=0;
        $insert['bonus_share']=$where['bonus_share'];
        $insert['total_share_num']=$where['total_share_num'];
        $insert['start_time']=date('Y-m-d H:i:s',$where['start_time']);
        $insert['end_time']=date('Y-m-d H:i:s',$where['end_time']);
        if($where['bonus_cycle_type']==$type){
            $shop_detail=ShopDetailModel::where(['shop_id'=>$where['shop_id'],'is_open_partner_manage'=>1])->find();
            if(!empty($shop_detail)){//判断合伙人系统是否开启
                $insert['status']=1;
            }
        }
        $insert['type']=$type;
        $insert['total_money']=$total_money;
        $insert['order_num']=$uo_info['all_num']+$ub_info['all_num'];
        $insert['max_money']=$max_money;
        $where['max_money']=$max_money;
        $where['total_money']=$total_money;
        $task_detail=$this->task_detail($where);
        $insert['cousume_bonus_ratio']=$task_detail['consume_complete_ratio'];
        $insert['toker_bonus_ratio']=$task_detail['toker_consume_complete_ratio'];
        $insert['promotion_bonus_ratio']=$task_detail['promotion_share_complete_ratio'];
        $insert['task_detail']=json_encode($task_detail);
        $s=[];
        $s['partner_id']=$where['partner_id'];
        $s['shop_id']=$where['shop_id'];
        $s['start_time']=date('Y-m-d H:i:s',$where['start_time']);
        $s['type']=$type;
        $PartnerBonusModel=new PartnerBonusModel();
        $info=$PartnerBonusModel->where($s)->find();
        if(empty($info)){
            $insert['add_time']=date('Y-m-d H:i:s');
            PartnerBonusModel::insert($insert);
        }else{
            PartnerBonusModel::up($insert,['partner_bonus_id'=>$info['partner_bonus_id']]);
        }
    }
    public function task_detail($where){
        $task_detail=PartnerTaskBonusSettingModel::where(['shop_id'=>$where['shop_id']])->find();
        if(empty($task_detail)) return $task_detail;
        $task_detail['consume_complete']=0;
        $task_detail['consume_complete_status']=0;
        $task_detail['consume_complete_ratio']=0;
        $task_detail['toker_consume_complete']=0;
        $task_detail['toker_consume_complete_status']=0;
        $task_detail['toker_consume_complete_ratio']=0;
        $task_detail['promotion_share_complete']=0;
        $task_detail['promotion_share_complete_status']=0;
        $task_detail['promotion_share_complete_ratio']=0;
        if($task_detail['status']==0) return $task_detail;
        $role_ids=explode(',',$task_detail['join_role_id']);
        if(!in_array($where['role_id'],$role_ids)){
            $task_detail['consume_complete_ratio']=$task_detail['consume_bonus_ratio'];
            $task_detail['promotion_share_complete_ratio']=$task_detail['promotion_bonus_ratio'];
            $task_detail['toker_consume_complete_ratio']=$task_detail['toker_bonus_ratio'];
            $task_detail['status']=0;
            return $task_detail;
        }
        if($task_detail['consume_task_type']==1){//消费任务开启
            if($task_detail['consume_type']==1){//单次金额
                $task_detail['consume_complete']=$where['max_money'];
                if($where['max_money']>=$task_detail['consume_price']){
                    $task_detail['consume_complete_status']=1;
                    $task_detail['consume_complete_ratio']=$task_detail['consume_bonus_ratio'];
                }
            }elseif ($task_detail['consume_type']==2){//合计金额
                $task_detail['consume_complete']=$where['total_money'];
                if($where['total_money']>=$task_detail['consume_price']){
                    $task_detail['consume_complete_status']=1;
                    $task_detail['consume_complete_ratio']=$task_detail['consume_bonus_ratio'];
                }
            }
        }else{
            $task_detail['consume_complete_ratio']=$task_detail['consume_bonus_ratio'];
        }
        if($task_detail['promotion_task_type']==1){//推广任务开启
            $num=PartnerShareModel::where(['partner_id'=>$where['partner_id'],'shop_id'=>$where['shop_id']])->count();
            $task_detail['promotion_share_complete']=$num;
            if($num>=$task_detail['promotion_share_num']){
                $task_detail['promotion_share_complete_status']=1;
                $task_detail['promotion_share_complete_ratio']=$task_detail['promotion_bonus_ratio'];
            }
        }else{
            $task_detail['promotion_share_complete_ratio']=$task_detail['promotion_bonus_ratio'];
        }
        if($task_detail['toker_task_type']==1){//拓客任务开启
            $where['pid']=$where['member_id'];
            unset($where['member_id']);
            if($task_detail['toker_consume_type']==1){//单次金额
                $where['status']=8;
                $g_money=GoodsOrderModel::max_money($where);
                $where['status']=1;
                $b_money=BuyOrderModel::max_money($where);
                $max_money=$g_money>$b_money?$g_money:$b_money;
                $task_detail['toker_consume_complete']=$max_money;
                if($max_money>=$task_detail['toker_consume_price']){
                    $task_detail['toker_consume_complete_status']=1;
                    $task_detail['toker_consume_complete_ratio']=$task_detail['toker_bonus_ratio'];
                }
            }elseif ($task_detail['toker_consume_type']==2){//合计金额
                $where['status']=8;
                $uo_info=GoodsOrderModel::user_order($where);
                $where['status']=1;
                $ub_info=BuyOrderModel::user_order($where);
                $total_money=$uo_info['money']+$ub_info['money'];
                $task_detail['toker_consume_complete']=$total_money;
                if($total_money>=$task_detail['toker_consume_price']){
                    $task_detail['toker_consume_complete_status']=1;
                    $task_detail['toker_consume_complete_ratio']=$task_detail['toker_bonus_ratio'];
                }
            }
        }else{
            $task_detail['toker_consume_complete_ratio']=$task_detail['toker_bonus_ratio'];
        }
        return $task_detail;
    }
}